package com.flycent.hqhs.ui;

import java.io.InputStream;
import java.util.List;

import org.json.JSONObject;

import net.tsz.afinal.FinalActivity;
import net.tsz.afinal.annotation.view.ViewInject;

import com.flycent.hqhs.App;
import com.flycent.hqhs.R;
import com.flycent.hqhs.bean.OrderTracking;
import com.flycent.hqhs.ui.view.AbsViewHolderAdapter;
import com.flycent.hqhs.util.TxtReaderUtil;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

/**
 * 订单追踪
 */
public class OrderTrackingActivity extends BackableTitleBarActivity{
	@ViewInject(id = R.id.order_track_number) TextView mOrderNumberText;
	@ViewInject(id = R.id.order_track_payment_terms) TextView mOrderPaymentTremsText;
	@ViewInject(id = R.id.order_track_express_way) TextView mOrderExpressWayText;
	@ViewInject(id = R.id.order_track_list) ListView mOrderTrackList;

	private OrderTrackingAdapter mAdapter;
	private List<OrderTracking> mOrderTrackings;



	@Override
	protected void onCreate(Bundle savedInstance) {
		super.onCreate(savedInstance);
		setContentView(R.layout.activity_order_tracking);
		FinalActivity.initInjectedView(this);
		getTitleBar().setTitle(R.string.title_order_tracking);

		mAdapter = new OrderTrackingAdapter(this, R.layout.item_order_tracking);
		mOrderTrackList.setAdapter(mAdapter);

		setDate();
		
	}

	private void setDate() {

		InputStream inputStream = getResources().openRawResource(R.raw.gson);
		String ss = TxtReaderUtil.getString(inputStream);
		App.showLog(ss);
		try {
			JSONObject jsonObject = new JSONObject(ss.toString());
			Gson gson = new Gson();
			mOrderTrackings = gson.fromJson(jsonObject.getJSONObject("result").getJSONArray("list").toString(),
					new TypeToken<List<OrderTracking>>(){}.getType());

			mAdapter.update(mOrderTrackings);
			mOrderNumberText.setText(jsonObject.getJSONObject("result").getString("no").toString());
			mOrderExpressWayText.setText(jsonObject.getJSONObject("result").getString("company").toString());

		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	public class OrderTrackingAdapter extends AbsViewHolderAdapter<OrderTracking>{

		public OrderTrackingAdapter(Context context, int layoutRes) {
			super(context, layoutRes);
		}

		@Override
		protected void bindData(int pos, OrderTracking itemData) {

			TextView expresstime = getViewFromHolder(R.id.expresstime);
			TextView expressdatails = getViewFromHolder(R.id.expressdatails);
			View mAbovelin = getViewFromHolder(R.id.above_lin);
			View mButtomlin = getViewFromHolder(R.id.buttom_lin);
			ImageView mImg = getViewFromHolder(R.id.order_left_point_img);
			
			expresstime.setText(itemData.getDatetime());
			expressdatails.setText(itemData.getRemark());

			if(pos == 0 ){
				mImg.setImageResource(R.drawable.order_track_arrive);
				mButtomlin.setVisibility(View.VISIBLE);
				mAbovelin.setVisibility(View.GONE);
			}else{
				mImg.setImageResource(R.drawable.order_track_point);
				if(pos == (mAdapter.getData().size()-1)){//
					mButtomlin.setVisibility(View.GONE);
					mAbovelin.setVisibility(View.VISIBLE);
				}else {
					mAbovelin.setVisibility(View.VISIBLE);
					mButtomlin.setVisibility(View.VISIBLE);
				}
			}
		
		}
	}
}
